import { defineStore } from "pinia";
import { ref } from "vue";
import { getCityListAPI } from '@/api/task'

export const taskStore = defineStore('task', () => {
// state
    // 城市列表和当前城市
    const cityList = ref([])
    const cityVal = ref(localStorage.getItem('city') ||'北京')
    const areaList = ref([])
    // 轮播图列表
    const bannerList = ref([])
    // 职位列表和当前职位
    const positionList = ref([])
    const positionVal = ref('')
    // 筛选列表
    const screenList = ref({})
    // 热搜列表
    const hotSearchList = ref([])

// action
    const getCityList = async() => {
        const res:any = await getCityListAPI()
        if (res) {
            cityList.value = res
            let city: { text: string; children: { text: string }[] }[] = [];
            for(var i = 0; i < cityList.value.length; i++) {
                city.push({
                    text: res[i].name,
                    children: []
                })
                if(res[i].children && res[i].children.length) {
                    for(var j = 0; j < res[i].children.length; j++) {
                        city[i].children.push({
                            text: res[i].children[j].name
                        })
                    }
                }
            }
            areaList.value = city
        }
    }
    const setCityList = (data: any) => {
        cityList.value = data
    }
    const setCityVal = (data: any) => {
        cityVal.value = data
    }

    const setbannerList = (data: any) => {
        bannerList.value = data
    }

    const setpositionList = (data: any) => {
        positionList.value = data
    }
    const setPositionVal = (data: any) => {
        positionVal.value = data
    }
    const setScreenList = (data: any) => {
        screenList.value = data
    }
    const setHotSearchList = (data: any) => {
        hotSearchList.value = data
    }

    return {
        cityList,
        setCityList,
        cityVal,
        setCityVal,
        bannerList,
        setbannerList,
        positionList,
        setpositionList,
        positionVal,
        setPositionVal,
        screenList,
        setScreenList,
        hotSearchList,
        setHotSearchList,
        areaList,
        getCityList
    }
})